package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.CameraParam;
import com.project.hqshorttaxi.entity.Camera;
import com.project.hqshorttaxi.mapper.CameraMapper;
import com.project.hqshorttaxi.service.CameraService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CameraServiceImpl implements CameraService {

    @Resource
    private CameraMapper cameraMapper;

    @Override
    public Camera getById(Integer id) {
        return cameraMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Camera entity) throws Exception {
        cameraMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Camera entity) throws Exception {
        cameraMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        cameraMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Camera> list(CameraParam param) {
        Example example = new Example(Camera.class);
        Example.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(param.getCameraNo())) {
            c.andLike("camera", "%" + param.getCameraNo() + "%");
        }
        if (param.getInstallType() != null) {
            c.andEqualTo("installType", param.getInstallType());
        }
        if (param.getParkingNo() != null) {
            c.andEqualTo("parkingNo", param.getParkingNo());
        }
        if (param.getCameraType() != null) {
            c.andEqualTo("cameraType", param.getCameraType());
        }
        example.orderBy("createTime").desc();
        List<Camera> list = cameraMapper.selectByExample(example);
        return list;
    }
}
